<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<th:block th:include="include :: summernote-css" />
<body class="gray-bg">
    
     <div id="myjobs" class="container-div">
		<div class="row">
			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table" data-mobile-responsive="true"></table>
			</div>
		</div>
	</div>
    <div th:include="include :: footer"></div>
    <script th:inline="javascript">
        var prefix = ctx + "job/sned";
		var snid="";var now0=Date.parse([[${now0}]].replace(/-/g,"/"));
        $(function() {
            var options = {
                url: prefix + "/list2",
                modalName: "已交作业",
				search: false,
		        showExport: false,
		        showSearch:false,
				showRefresh:false,
				showColumns:false,
				showToggle:false,
				sortName: "sntime",    // 排序列名称
                sortOrder: "desc",
                columns: [{
		            checkbox: true
		        },
				{
					field : 'id', 
					title : '已交ID'
				},
				{
					field : 'snpub.title', 
					title : '作业名称'
				},
				{
					field : 'sntime', 
					title : '提交时间'
				},
				{
					field : 'score', 
					title : '得分'
				},
				{
					field : 'comment', 
					title : '状态'
				},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		            	var actions = [];
		            	var snendtime=row.snpub.snendtime;		            	
		            	snendtime=Date.parse(snendtime);
		            	if(row.score>0) 
		            		actions.push('<a class="btn btn-danger btn-xs " href="#"><i class="fa fa-lock"></i> 结束</a> ');
		            	else if(now0>snendtime){
		            		if(row.isrejob)
		            			actions.push('<a class="btn btn-danger btn-xs " href="#"onclick="postjob(\'' + row.snid + '\',\''+Base64.encode(row.snpub.description)+ '\')"><i class="fa fa-edit"></i> 补交</a> ');
		            		else
		            			actions.push('<a class="btn btn-danger btn-xs " href="#"><i class="fa fa-lock"></i> 截止</a> ');
		            	}
		            	else if(row.content!=null && row.content.trim().length>30)
		            		actions.push('<a class="btn btn-info btn-xs " href="#" onclick="postjob(\'' + row.snid + '\',\''+Base64.encode(row.snpub.description)+'\')"><i class="fa fa-edit"></i> 重新提交</a> ');
		            	else
		            		actions.push('<a class="btn btn-info btn-xs " href="#" onclick="postjob(\'' + row.snid +'\',\''+Base64.encode(row.snpub.description)+ '\')"><i class="fa fa-edit"></i> 提交</a> ');
		            	if(row.content!=null && row.content.trim().length>30)
							actions.push('<a class="btn btn-primary btn-xs " href="#" onclick="lookmyjob(\''+ Base64.encode(row.content) +'\',\''+Base64.encode(row.snpub.description)+  '\')"><i class="fa fa-eye"></i> 查看</a> ');
						return actions.join('');
		            }
		        }]
            };
            $.table.init(options);
        });
        function lookmyjob(id,desc){
        	$("#myeditor").show();
    		$("#myjobs").hide();
    		$("#save").hide();
    		$("#editortitle").html("查看作业");
    		$("#description").html(Base64.decode(desc));
    		$("#cancel").text("返回");
    		$("#eg").removeClass("no-padding");
            $('.click2edit').summernote('destroy');
            $('.click2edit').html(Base64.decode(id))
            
            
        }
    </script>
        <div id="myeditor" class="wrapper wrapper-content">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title" style="text-align: center;">
                        <h3 id="editortitle">提交作业</h3>                        
                        <button id="save" class="btn btn-primary  btn-lg" onclick="save()" type="button">提交</button>
                        <button id="cancel" class="btn btn-danger btn-lg" onclick="cancel()" type="button">取消</button>
                    </div>
					<div class="form-group">
						<label class="col-sm-3 control-label">作业要求：</label>
						<div class="col-sm-12">
							<div id="description"></div>
						</div>
					</div>

					<div class="ibox-content no-padding" id="eg">
                        <div class="click2edit"></div>
                    </div>
                </div>
            </div>
        </div>
    
    </div>    
    <th:block th:include="include :: summernote-js" />
    <script>
    $(function(){
    	$("#myeditor").hide();
            //$("#myjobs").show();
    })
        function createsn() {
            $('.click2edit').summernote({                
                //focus: true,
                height : '220px',
                lang : 'zh-CN',
                placeholder:"在此编辑你的作业，可以图文混排",
                callbacks: {
                    onImageUpload: function(files) {
                    	uploadpic(files[0]);                        
                    }
                }
            });
            
        };
        function uploadpic(file){
        	var formData = new FormData();
            formData.append("file", file);
            formData.append("filetype", "img");
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                    	var imgNode = document.createElement("img");
			            imgNode.src = ctx+"common/vimg?n="+result.url;
			            $('.click2edit').summernote('insertNode', imgNode);
                        $(".note-image-input").removeAttr("name");
                    } else {
                        $.modal.alertError(result.msg);
                        $(".note-image-input").removeAttr("name");
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }
        function save() {
            var aHTML = $('.click2edit').summernote('code');
            if(aHTML.trim().length<30 || snid.length<5) return;
            var config = {
        	        url: ctx + 'job/sned/edit2',
        	        type: "post",
        	        dataType: "json",
        	        data: {"snid":snid,"content":Base64.encode(aHTML)},
        	        beforeSend: function () {
        	        	$.modal.loading("正在处理中，请稍后...");
        	        },
        	        success: function(result) {
        	        	if (result.code == web_status.SUCCESS){
        	        		$.modal.msgSuccess(result.msg);
        	        		$('.click2edit').summernote('reset');
        	        		$.modal.closeLoading();
        	        		snid="";
        	        		$("#eg").removeClass("no-padding");
        	                $('.click2edit').summernote('destroy');
        	        		$("#myeditor").hide();
        	        		$("#myjobs").show();
        	        		$.table.refresh();
        	        		
        	        		//var url = ctx + 'job/sned/myjobs';
        	    			//$.modal.parentTab("我的作业", url);
        	        	}else{
        	        		$.modal.alertError(result.msg);
        	        		$.modal.closeLoading();
        	        	}        	        	
        	        },
                    error: function(error) {
                        $.modal.alertWarning("提交作业失败。");
                    }
        	    };
        	    $.ajax(config);
        };
        function cancel() {
        	$("#eg").removeClass("no-padding");
            $('.click2edit').summernote('destroy');
        	$("#myeditor").hide();
    		$("#myjobs").show();
    		$('.click2edit').html("");
    		$("#description").html("")
    		
        }
        function postjob(id,desc) {
        	$("#myeditor").show();
    		$("#myjobs").hide();
    		$("#save").show();
    		$("#editortitle").html("提交作业");
    		$("#description").html(Base64.decode(desc));
    		$("#cancel").text("取消");
    		$('.click2edit').html("");
    		snid=id;
    		$("#eg").addClass("no-padding");
    		createsn();
        }
    </script>
</body>
</html>
